
package cn.stylefeng.roses.kernel.saas.controller;

import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginRequest;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.saas.entity.SysTenant;
import cn.stylefeng.roses.kernel.saas.request.SysTenantRequest;
import cn.stylefeng.roses.kernel.saas.service.TenantService;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 租户信息控制器
 *
 * @author fengshuonan
 * @date 2021/5/26 17:30
 */
@RestController
@ApiResource(name = "租户控制器")
public class TenantController {

    @Resource
    private TenantService tenantService;

    /**
     * 租户注册
     *
     * @author fengshuonan
     * @date 2021/5/26 17:30
     */
    @PostResource(name = "租户注册", path = "/sysTenant/register")
    public ResponseData register(@RequestBody @Validated(SysTenantRequest.add.class) SysTenantRequest sysTenantRequest) {
        tenantService.register(sysTenantRequest);
        return new SuccessResponseData();
    }

    /**
     * 修改租户
     *
     * @author fengshuonan
     * @date 2021/5/26 17:30
     */
    @PostResource(name = "修改租户", path = "/sysTenant/edit")
    public ResponseData edit(@RequestBody SysTenantRequest sysTenantRequest) {
        tenantService.edit(sysTenantRequest);
        return new SuccessResponseData();
    }

    /**
     * 删除租户
     *
     * @author fengshuonan
     * @date 2021/5/26 17:30
     */
    @PostResource(name = "删除租户", path = "/sysTenant/delete")
    public ResponseData delete(@RequestBody SysTenantRequest sysTenantRequest) {
        tenantService.del(sysTenantRequest);
        return new SuccessResponseData();
    }

    /**
     * 查询租户列表（分页）
     *
     * @author fengshuonan
     * @date 2021/5/26 17:30
     */
    @GetResource(name = "查询租户列表（分页）", path = "/sysTenant/page")
    public ResponseData listPage(SysTenantRequest sysTenantRequest) {
        PageResult<SysTenant> page = tenantService.findPage(sysTenantRequest);
        return new SuccessResponseData(page);
    }

    /**
     * 获取租户下拉列表（用来选择租户）
     *
     * @author fengshuonan
     * @date 2021/5/26 17:39
     */
    @GetResource(name = "获取租户下拉列表", path = "/sysTenant/tenantDropdown", requiredPermission = false, requiredLogin = false)
    public ResponseData tenantDropdown() {
        List<SysTenant> dropDownList = tenantService.getDropDownList();
        return new SuccessResponseData(dropDownList);
    }

    /**
     * 账号密码租户编码登录
     *
     * @param tenantRequest 账号 密码 租户code
     * @return 登录的token
     * @author fengshuonan
     * @date 2021/5/26 17:53
     */
    @PostResource(name = "账号密码租户编码登录", path = "/sysTenant/tenantCodeLogin", requiredPermission = false, requiredLogin = false)
    public ResponseData tenantCodeLogin(@RequestBody LoginRequest tenantRequest) {
        return new SuccessResponseData(tenantService.tenantCodeLogin(tenantRequest));
    }

    /**
     * 检查企业名称是否存在
     *
     * @author fengshuonan
     * @date 2021/5/26 17:53
     */
    @GetResource(name = "检查企业名称是否存在", path = "/sysTenant/checkCompanyName", requiredPermission = false, requiredLogin = false)
    public ResponseData checkCompanyName(@Validated(SysTenantRequest.checkCompanyName.class) SysTenantRequest sysTenantRequest) {
        boolean flag = tenantService.checkCompanyName(sysTenantRequest);
        return new SuccessResponseData(flag);
    }

}
